package com.gongpingjialoan.view;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.bean.car.SortBean;
import com.gongpingjialoan.data.NewCarBrand;
import com.gongpingjialoan.utility.Utils;
import com.gongpingjialoan.widget.BrandPopupWindow;
import com.gongpingjialoan.widget.PriceSortPopopWindow;

import java.util.List;


/**
 * Created by Administrator on 2016/5/17.
 */
public class NewCarSortLinearLayout extends LinearLayout implements View.OnClickListener {

    private TextView priceTextView, brandTextView;

    private View priceView, sortView;

    private GeCarList mGeCarList;

    private Fragment carListFragment;

    private PriceSortPopopWindow mPriceSortPopopWindow;

    private BrandPopupWindow mBrandPopupWindow;

    private String mSort = "", mBrand = "";


    public NewCarSortLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        LayoutInflater.from(getContext()).inflate(R.layout.new_car_sort_layout, this, true);
        priceView = findViewById(R.id.sort_price_ll);
        sortView = findViewById(R.id.brand_ll);
        priceTextView = (TextView) findViewById(R.id.sort_price_tv);
        brandTextView = (TextView) findViewById(R.id.brand_tv);
        priceView.setOnClickListener(this);
        sortView.setOnClickListener(this);

        mBrandPopupWindow = new BrandPopupWindow(getContext());
        mBrandPopupWindow.setISelctedPrice(new BrandPopupWindow.ISelctedPrice() {
            @Override
            public void select(String brand ,String brand_slug) {
                if (TextUtils.isEmpty(brand_slug) ||"全部品牌".equals(brand)) {
                    brandTextView.setText("全部品牌");
                    Utils.setTextViewImage(getContext(), brandTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                } else {
                    brandTextView.setText(brand);
                    Utils.setTextViewImage(getContext(), brandTextView, R.drawable.sort_focus_down_new, R.color.new_color);
                }
                mBrand = brand_slug;
                mGeCarList.getCarList(mSort, mBrand);
            }
        });

        mPriceSortPopopWindow = new PriceSortPopopWindow(getContext());
        mPriceSortPopopWindow.setISelcted(new PriceSortPopopWindow.ISelcted() {
            @Override
            public void select(SortBean bean) {
                if (bean != null) {
                    mSort = bean.getValue();
                }
                mGeCarList.getCarList(mSort, mBrand);
                if (TextUtils.isEmpty(mSort)) {
                    priceTextView.setText("价格排序");
                    Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_normal_new, R.color.text_details_color);
                } else {
                    priceTextView.setText(bean.getTitle());
                    Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_focus_down_new, R.color.new_color);
                }
            }
        });

    }


    public  void setBrands(List<NewCarBrand> brandBean){
        mBrandPopupWindow.setBrands(brandBean);
    }







    public void setPosition(int pos) {
        if (mPriceSortPopopWindow != null) {
            mPriceSortPopopWindow.setPosition(pos);
        }

    }


    @Override
    public void onClick(View v) {
        View parent = findViewById(R.id.line);
        if (v == priceView) {
            Utils.showAsDropDown(parent,mPriceSortPopopWindow);
        } else if (v == sortView) {
            Utils.showAsDropDown(parent,mBrandPopupWindow);
        }

    }




    public void setGeCarList(GeCarList geCarList) {
        mGeCarList = geCarList;
        carListFragment = (Fragment) geCarList;
    }


    public void clearPrice() {
        mBrand = "";
        mSort ="";
        Utils.setTextViewImage(getContext(), priceTextView, R.drawable.sort_normal_new, R.color.text_details_color);
    }


    public interface GeCarList {
         void getCarList(String sort, String brand);
    }


}
